package com.example.bzh.gamecat.ui.live.hot;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.print.PrintHelper;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.bean.hot.BannerListBean;
import com.example.bzh.gamecat.bean.hot.DataBean;
import com.example.bzh.gamecat.bean.hot.Hotbean;
import com.example.bzh.gamecat.bean.hot.RecommendMasterListBean;
import com.example.bzh.gamecat.httputils.hotlive.Hotdata;
import com.example.bzh.gamecat.ui.live.newest.show.ShowActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class HotFragment extends Fragment {

    @BindView(R.id.vp_hot_live)
    ViewPager vpHotLive;
    @BindView(R.id.rv_hot_live)
    RecyclerView rvHotLive;
    private HotViewpageAdapter adapter;
    private HotRecycleviewAdapter recycleviewAdapter;
    private LinearLayoutManager manager;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                DataBean dataBean = (DataBean) msg.obj;
                List<BannerListBean> bannerList = dataBean.getBannerList();
                adapter = new HotViewpageAdapter(bannerList);
                vpHotLive.setAdapter(adapter);

                manager = new LinearLayoutManager(getActivity());
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                rvHotLive.setLayoutManager(manager);
                rvHotLive.setNestedScrollingEnabled(false);

                final List<RecommendMasterListBean> recommendMasterList = dataBean.getRecommendMasterList();
                recycleviewAdapter = new HotRecycleviewAdapter(R.layout.hot_recycleview_item, recommendMasterList);
                rvHotLive.setAdapter(recycleviewAdapter);

                recycleviewAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
                    @Override
                    public void onItemClick(View view, int i) {
                        Intent intent = new Intent(getActivity(), ShowActivity.class);
                        intent.putExtra("url", recommendMasterList.get(i).getPullUrl());
                        startActivity(intent);
                    }
                });
            }
        }
    };


    public HotFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hot, container, false);
        ButterKnife.bind(this, view);
        //初始化presenter，并且请求网络放完获取数据
        String url = "http://app.show.youximao.tv/m/getRecommendMasters.action?current=0&pageSize=20&token=46196dac1b268efebd0df5247f490e25&ppinf=0%7C1471350757%7C0%7CdWlkOjkyNjkzNHxuaWNrbmFtZTolRTQlQjglODAlRTklOUQlQTIlRTQlQjglODAlRTclOUIlQjQlRTQlQjglODAlRTclOTQlOUZ8dmVyaWZpZWQ6MHxyZWd0aW1lOjE0NzEzNTA3NTd8dXNlcm5hbWU6MTM0MTI2MQ";
        Hotdata.getHotData(getActivity(), url, handler);

        return view;
    }
}
